//
//  ViewController.m
//  BaseProject
//
//  Created by itzhaolei on 2024/4/23.
//

#import "ViewController.h"
#import "ASectionController.h"
#import "BSectionController.h"
#import "CSectionController.h"
#import "DSectionController.h"
#import "PageListCoordinator.h"
#import "CommonSectionAdapter.h"

@interface ViewController ()<CoordinatorDataSource>

/// 滑动视图
@property (nonatomic, strong) IGListCollectionView *collectionView;
/// 协调器
@property (nonatomic, strong) PageListCoordinator *coordinator;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.coordinator.adapter.collectionView = self.collectionView;
    [self addSubviews];
    [self addConstraints];
    [self addActions];
}

#pragma mark - Add
- (void)addSubviews {
    [self.view addSubview:self.collectionView];
}

#pragma mark - Constraints
- (void)addConstraints {
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - Actions
- (void)addActions {
    MJWeakSelf;
    // 刷新所有区
    [self.coordinator.reloadAllSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf.coordinator resetData];
    }];
}

#pragma mark - CoordinatorDataSource
// 适配器管理的区域对应的模型
- (NSArray<CommonSectionAdapter *> *)objectsForListAdapter:(IGListAdapter *)listAdapter {
    return @[
        [[CommonSectionAdapter alloc] init],
        [[CommonSectionAdapter alloc] init],
        [[CommonSectionAdapter alloc] init],
        [[CommonSectionAdapter alloc] init]
    ];
}
// 根据object，加载指定区域的SectionController
- (CommonSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(CommonSectionAdapter *)sectionAdapter {
    if (!sectionAdapter.section) {
        return [[ASectionController alloc] initWithCoordinator:self.coordinator];
    }
    if (sectionAdapter.section == 1) {
        return [[BSectionController alloc] initWithCoordinator:self.coordinator];
    }
    if (sectionAdapter.section == 2) {
        return [[CSectionController alloc] initWithCoordinator:self.coordinator];
    }
    return [[DSectionController alloc] initWithCoordinator:self.coordinator];
}
- (nullable UIView *)emptyViewForListAdapter:(IGListAdapter *)listAdapter {
    return nil;
}

#pragma mark - Lazy
- (IGListCollectionView *)collectionView {
    if (!_collectionView) {
        IGListCollectionViewLayout *flowLayout = [[IGListCollectionViewLayout alloc] initWithStickyHeaders:false scrollDirection:UICollectionViewScrollDirectionVertical topContentInset:0 stretchToEdge:NO];
        IGListCollectionView *view = [[IGListCollectionView alloc]initWithFrame:CGRectZero listCollectionViewLayout:flowLayout];
        view.backgroundColor = [UIColor clearColor];
        view.showsVerticalScrollIndicator = NO;
        view.showsHorizontalScrollIndicator = NO;
        _collectionView = view;
    }
    return _collectionView;
}
- (PageListCoordinator *)coordinator {
    if (!_coordinator) {
        PageListCoordinator *coordinator = [[PageListCoordinator alloc] initWithDelegate:self];
        _coordinator = coordinator;
    }
    return _coordinator;
}

@end
